package cn.com.generaldata.os.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.nutz.castor.Castors;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.Exps;
import org.nutz.el.El;
import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.nutz.lang.random.R;
import org.nutz.lang.util.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.generaldata.jsme_indicator_system.entity.Dustry;
import cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccountingReference;
import cn.com.generaldata.jsme_indicator_system.entity.IndicatorConsumpt;
import cn.com.generaldata.jsme_indicator_system.entity.IndicatorProduction;
import cn.com.generaldata.os.constant.Constants;
import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.impl.IndicatorDAO;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.newpojo.IndicatorAccounting;
import cn.com.generaldata.os.newpojo.IndicatorInput;
import cn.com.generaldata.os.newpojo.SystemCodedic;
import cn.com.generaldata.os.paging.Event;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.paging.Page;
import cn.com.generaldata.os.paging.Result;
import cn.com.generaldata.os.pojo.Area;
import cn.com.generaldata.os.pojo.IndicatorAccount;
import cn.com.generaldata.os.pojo.IndicatorAccountView;
import cn.com.generaldata.os.pojo.IndicatorData;
import cn.com.generaldata.os.pojo.IndicatorEvaluationView;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.service.intf.IndicatorServiceInterface;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;
import cn.com.generaldata.os.vo.IndicatorDataVO;
import cn.com.generaldata.os.vo.IndicatorInputVO;

/**
 * 指标录入Service.
 */
public class IndicatorService implements IndicatorServiceInterface {
	private static Logger log = LoggerFactory.getLogger(IndicatorService.class);
	private BaseDAOInterface<IndicatorInput> indicatorInputDAO;
	@Autowired
	private Dao indicatorSystemDao;
	@Autowired
	private Dao dao;
	private IndicatorDAO indicatorDAOIpml;
	private static final String provinceAreaId = Constants.PROVINCE_AREA_ID;
	private static final HashMap areamap = new HashMap<String, String>() {
		{
			put("天津市", "120000000000000");
			put("辽宁省", "210000000000000");
			put("河北省", "130000000000000");
			put("浙江省", "330000000000000");
			put("上海市", "310000000000000");
			put("福建省", "350000000000000");
			put("山东省", "370000000000000");
			put("广东省", "440000000000000");
			put("广西壮族自治区", "450000000000000");
			put("海南省", "460000000000000");
			put("全国", "0");
			put("江苏省", "320000000000000");
			put("南通市", "320600000000000");
			put("盐城市", "320900000000000");
			put("连云港市", "320700000000000");
		}
	};

	public void setIndicatorDAOIpml(IndicatorDAO indicatorDAOIpml) {
		this.indicatorDAOIpml = indicatorDAOIpml;
	}

	@Autowired
	private SqlTemplate indicatorSystemSqlTemplate;

	@Autowired
	private IndicatorEvaluationService indicatorEvaluationService;

	private BaseDAOInterface<IndicatorAccounting> indicatorAccountingDAO;

	private BaseDAOInterface<IndicatorData> indicatorDAO;

	public void setIndicatorAccountingDAO(BaseDAOInterface<IndicatorAccounting> indicatorAccountingDAO) {
		this.indicatorAccountingDAO = indicatorAccountingDAO;
	}

	private BaseDAOInterface<IndicatorEvaluationView> indicatorEvaluationDAO;

	public void setIndicatorEvaluationDAO(BaseDAOInterface<IndicatorEvaluationView> indicatorEvaluationDAO) {
		this.indicatorEvaluationDAO = indicatorEvaluationDAO;
	}

	private BaseDAOInterface<IndicatorAccount> indicatorAccountDAO;

	private BaseDAOInterface<IndicatorAccountView> indicatorAccountViewDAO;

	public void setIndicatorAccountViewDAO(BaseDAOInterface<IndicatorAccountView> indicatorAccountViewDAO) {
		this.indicatorAccountViewDAO = indicatorAccountViewDAO;
	}

	private BaseDAOInterface<Area> areaDAO;

	public void setIndicatorAccountDAO(BaseDAOInterface<IndicatorAccount> indicatorAccountDAO) {
		this.indicatorAccountDAO = indicatorAccountDAO;
	}

	private CalcuIndicatorAccoutService calcuIndicatorAccoutService;

	public void setCalcuIndicatorAccoutService(CalcuIndicatorAccoutService calcuIndicatorAccoutService) {
		this.calcuIndicatorAccoutService = calcuIndicatorAccoutService;
	}

	/**
	 * 查询指标项
	 *
	 * @param name
	 * @param frequency
	 * @return
	 */
	@Override
	public Result findIndicatorInputs(String type, String name, Integer frequency, Page page) {

		Filter filter = new Filter();
		if (type != null && !"".equals(type)) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "category", Integer.valueOf(type)));
		}
		if (name != null && !"".equals(name)) {
			filter.addRule(new FilterRule(FilterConstant.LIKE, "name", "%" + name + "%"));
		}
		if (frequency != null && frequency != -1) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "frequency", Integer.valueOf(frequency)));
		}
		int count = indicatorInputDAO.count(IndicatorInput.class, filter);
		List<IndicatorInput> find = null;
		if (page == null) {
			find = indicatorInputDAO.find(IndicatorInput.class, filter, null);
			if (find != null && find.size() > 0) {
				List<IndicatorInputVO> list = new ArrayList<IndicatorInputVO>();
				for (IndicatorInput input : find) {
					IndicatorInputVO inputVO = BeanMapper.map(input, IndicatorInputVO.class);
					if (input.getFrequency() == 1) {
						inputVO.setTimeType("年报");
					} else if (input.getFrequency() == 0) {
						inputVO.setTimeType("月报");
					} else if (input.getFrequency() == 2) {
						inputVO.setTimeType("季报");
					} else if (input.getFrequency() == 3) {
						inputVO.setTimeType("半年报");
					}
					list.add(inputVO);
				}

				return new Result(1, 1, count, list, new ArrayList<Event>());
			}
		} else {
			find = indicatorInputDAO.find(IndicatorInput.class, filter, page);
			if (find != null && find.size() > 0) {
				List<SystemCodedic> types = queryType();
				List<IndicatorInputVO> list = new ArrayList<IndicatorInputVO>();
				for (IndicatorInput input : find) {
					IndicatorInputVO inputVO = BeanMapper.map(input, IndicatorInputVO.class);
					if (input.getFrequency() == 1) {
						inputVO.setTimeType("年报");
					} else if (input.getFrequency() == 0) {
						inputVO.setTimeType("月报");
					} else if (input.getFrequency() == 2) {
						inputVO.setTimeType("季报");
					} else if (input.getFrequency() == 3) {
						inputVO.setTimeType("半年报");
					}
					for (SystemCodedic sc : types) {
						if (sc.getCode().equals(input.getCategory().toString())) {
							inputVO.setCategoryName(sc.getCodeName());
							break;
						}
					}
					list.add(inputVO);
				}
				return new Result(page.getPageNumber(), page.getPageSize(), count, list, new ArrayList<Event>());
			}
		}
		return null;

	}

	public void setIndicatorInputDAO(BaseDAOInterface<IndicatorInput> indicatorInputDAO) {
		this.indicatorInputDAO = indicatorInputDAO;
	}

	public void setIndicatorDAO(BaseDAOInterface<IndicatorData> indicatorDAO) {
		this.indicatorDAO = indicatorDAO;
	}

	public void setAreaDAO(BaseDAOInterface<Area> areaDAO) {
		this.areaDAO = areaDAO;
	}

	/**
	 * 录入指标值
	 *
	 * @param indiVo
	 * @return
	 */
	@Override
	public String createIndicator(IndicatorDataVO indiVo) {
		IndicatorData indicator = BeanMapper.map(indiVo, IndicatorData.class);
		User user = new User();
		String createPre = indiVo.getCreatePreId();
		user.setId(createPre);
		indicator.setCreatePre(user);
		if ("".equals(indiVo.getAreaId())) {
			indicator.setAreaId(null);
		}
		indicator.setId(R.UU64());
		indicatorDAO.save(indicator);
		return indicator.getId();
	}

	@Override
	public Result queryIndicatorData(IndicatorDataVO indiVo, Page page) {
		return indicatorDAOIpml.queryIndicatorData(indiVo, page);
	}

	@Override
	public List<SystemCodedic> queryType() {
		return indicatorDAOIpml.queryType();
	}

	@Override
	public Result queryIndicatorAccount(IndicatorAccountVO indiVo, Page page, String sort, String order) {
		Filter filter = new Filter();
		if (indiVo != null) {
			if (indiVo.getAreaId() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", indiVo.getAreaId()));
			}
			if (indiVo.getTimeType() != null && !"".equals(indiVo.getTimeType().toString())) {
				// 半年报
				if (!"3".equals(indiVo.getTimeType().toString())) {
					filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType", indiVo.getTimeType()));
				} else {
					filter.addRule(new FilterRule(FilterConstant.EQUAL, "yearTime", indiVo.getYearTime()));
				}
			}

			if (indiVo.getYear() != null && indiVo.getYear() != 0) {
				if (indiVo.getCategory2() != null && indiVo.getCategory2() != 0) {
					filter.addRule(new FilterRule(FilterConstant.GE, "year", indiVo.getYear()));
					filter.addRule(new FilterRule(FilterConstant.LE, "year", indiVo.getCategory2()));
				} else {
					filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", indiVo.getYear()));
				}
			}
			if (indiVo.getMonth() != null && indiVo.getMonth() != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "month", indiVo.getMonth()));
			}
			if (indiVo.getQuarter() != null && indiVo.getQuarter() != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "quarter", indiVo.getQuarter()));
			}
		}

		int count = indicatorAccountViewDAO.count(IndicatorAccountView.class, filter);
		DecimalFormat nf = new DecimalFormat();
		nf.setMinimumFractionDigits(2);
		nf.setMaximumFractionDigits(2);
		List<IndicatorAccountView> find = null;
		if (sort != null && !"".equals(sort)) {
			if ("desc".equals(order)) {
				filter.setOrder(FilterConstant.DESC);
			} else {
				filter.setOrder(FilterConstant.ASC);
			}
			filter.setSortBy("editValueGrowthRate");
		}
		if (page == null) {
			find = indicatorAccountViewDAO.find(IndicatorAccountView.class, filter, null);
			if (find != null && find.size() > 0) {
				List<IndicatorAccountVO> list = new ArrayList<IndicatorAccountVO>();
				for (IndicatorAccountView input : find) {
					IndicatorAccounting indicatorInput = indicatorAccountingDAO.get(IndicatorAccounting.class,
							input.getIndicatorId());
					String indicatorValue = input.getIndicatorValue() == null ? "" : nf.format(input
							.getIndicatorValue());
					String editValue = input.getEditValue() == null ? "" : nf.format(input.getEditValue());
					String editValueGrowthRate = input.getEditValueGrowthRate() == null ? "" : nf.format(input
							.getEditValueGrowthRate());
					String indicatorValueGrowthRate = input.getIndicatorValueGrowthRate() == null ? "" : nf
							.format(input.getIndicatorValueGrowthRate());
					String ivYearBookGrowthRate = input.getIvYearBookGrowthRate() == null ? "" : nf.format(input
							.getIvYearBookGrowthRate());
					String evYearBookGrowthRate = input.getEvYearBookGrowthRate() == null ? "" : nf.format(input
							.getEvYearBookGrowthRate());
					String ivlastYearBookGrowthRate = input.getIvLastYearBookGrowthRate() == null ? "" : nf
							.format(input.getIvLastYearBookGrowthRate());
					String evlastYearBookGrowthRate = input.getEvLastYearBookGrowthRate() == null ? "" : nf
							.format(input.getEvLastYearBookGrowthRate());
					IndicatorAccountVO inputVO = BeanMapper.map(input, IndicatorAccountVO.class);
					inputVO.setIndicatorName(indicatorInput.getName());
					inputVO.setIndicatorUnit(indicatorInput.getUnit());
					inputVO.setIndicatorValue(indicatorValue);
					inputVO.setEditValue(editValue);
					inputVO.setEditValueGrowthRate(editValueGrowthRate);
					inputVO.setIndicatorValueGrowthRate(indicatorValueGrowthRate);
					inputVO.setIvYearBookGrowthRate(ivYearBookGrowthRate);
					inputVO.setEvYearBookGrowthRate(evYearBookGrowthRate);
					inputVO.setIvLastYearBookGrowthRate(ivlastYearBookGrowthRate);
					inputVO.setEvLastYearBookGrowthRate(evlastYearBookGrowthRate);
					list.add(inputVO);
				}

				return new Result(1, 1, count, list, new ArrayList<Event>());
			}
		} else {
			find = indicatorAccountViewDAO.find(IndicatorAccountView.class, filter, page);
			if (find != null && find.size() > 0) {
				List<IndicatorAccountVO> list = new ArrayList<IndicatorAccountVO>();
				List<Integer> indicators = new ArrayList<Integer>();
				for (IndicatorAccountView input : find) {
					IndicatorAccounting indicatorInput = indicatorAccountingDAO.get(IndicatorAccounting.class,
							input.getIndicatorId());
					String indicatorValue = input.getIndicatorValue() == null ? "" : nf.format(input
							.getIndicatorValue());
					String editValue = input.getEditValue() == null ? "" : nf.format(input.getEditValue());
					String editValueGrowthRate = input.getEditValueGrowthRate() == null ? "" : nf.format(input
							.getEditValueGrowthRate());
					String indicatorValueGrowthRate = input.getIndicatorValueGrowthRate() == null ? "" : nf
							.format(input.getIndicatorValueGrowthRate());
					String ivYearBookGrowthRate = input.getIvYearBookGrowthRate() == null ? "" : nf.format(input
							.getIvYearBookGrowthRate());
					String evYearBookGrowthRate = input.getEvYearBookGrowthRate() == null ? "" : nf.format(input
							.getEvYearBookGrowthRate());
					String ivlastYearBookGrowthRate = input.getIvLastYearBookGrowthRate() == null ? "" : nf
							.format(input.getIvLastYearBookGrowthRate());
					String evlastYearBookGrowthRate = input.getEvLastYearBookGrowthRate() == null ? "" : nf
							.format(input.getEvLastYearBookGrowthRate());
					if (indicatorInput == null) {
						indicators.add(input.getIndicatorId());
					} else {
						IndicatorAccountVO inputVO = BeanMapper.map(input, IndicatorAccountVO.class);
						inputVO.setIndicatorName(indicatorInput.getName());
						inputVO.setIndicatorUnit(indicatorInput.getUnit());
						inputVO.setIndicatorValue(indicatorValue);
						inputVO.setEditValue(editValue);
						inputVO.setEditValueGrowthRate(editValueGrowthRate);
						inputVO.setIndicatorValueGrowthRate(indicatorValueGrowthRate);
						inputVO.setIvYearBookGrowthRate(ivYearBookGrowthRate);
						inputVO.setEvYearBookGrowthRate(evYearBookGrowthRate);
						inputVO.setIvLastYearBookGrowthRate(ivlastYearBookGrowthRate);
						inputVO.setEvLastYearBookGrowthRate(evlastYearBookGrowthRate);

						list.add(inputVO);
					}
				}
				for (Integer i : indicators) {
					System.out.println("空的指标ID: " + i);
				}
				return new Result(page.getPageNumber(), page.getPageSize(), count, list, new ArrayList<Event>());
			}
		}
		return null;
	}

	/**
	 * 查询评估指标值
	 *
	 * @param indiVo
	 * @param page
	 * @return
	 */
	@Override
	public Result queryIndicatorEvaluation(IndicatorAccountVO indiVo, Page page) {
		Filter filter = new Filter();
		if (indiVo != null) {
			if (indiVo.getCategory1() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "category1", indiVo.getCategory1()));
			}
			if (indiVo.getCategory2() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "category2", indiVo.getCategory2()));
			}
			if (indiVo.getAreaId() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", indiVo.getAreaId()));
			}
			if (indiVo.getTimeType() != null && !"".equals(indiVo.getTimeType().toString())) {
				// 半年报
				if (!"3".equals(indiVo.getTimeType().toString())) {
					filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType", indiVo.getTimeType()));
				} else {
					filter.addRule(new FilterRule(FilterConstant.EQUAL, "yearTime", indiVo.getYearTime()));
				}
			}
			if (indiVo.getYear() != null && indiVo.getYear() != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", indiVo.getYear()));
			}
			if (indiVo.getMonth() != null && indiVo.getMonth() != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "month", indiVo.getMonth()));
			}
			if (indiVo.getQuarter() != null && indiVo.getQuarter() != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "quarter", indiVo.getQuarter()));
			}
		}

		int count = indicatorEvaluationDAO.count(IndicatorEvaluationView.class, filter);
		DecimalFormat nf = new DecimalFormat();
		nf.setMinimumFractionDigits(2);
		nf.setMaximumFractionDigits(2);
		List<IndicatorEvaluationView> find = null;
		if (page == null) {
			find = indicatorEvaluationDAO.find(IndicatorEvaluationView.class, filter, null);
			if (find != null && find.size() > 0) {
				List<IndicatorAccountVO> list = new ArrayList<IndicatorAccountVO>();
				for (IndicatorEvaluationView input : find) {
					String indicatorValue = input.getIndicatorValue() == null ? "" : nf.format(input
							.getIndicatorValue());
					String editValue = input.getEditValue() == null ? "" : nf.format(input.getEditValue());

					IndicatorAccountVO inputVO = BeanMapper.map(input, IndicatorAccountVO.class);
					inputVO.setIndicatorName(input.getName());
					inputVO.setIndicatorUnit(input.getUnit());
					inputVO.setIndicatorValue(indicatorValue);
					inputVO.setEditValue(editValue);
					list.add(inputVO);
				}

				return new Result(1, 1, count, list, new ArrayList<Event>());
			}
		} else {
			find = indicatorEvaluationDAO.find(IndicatorEvaluationView.class, filter, page);
			if (find != null && find.size() > 0) {
				List<IndicatorAccountVO> list = new ArrayList<IndicatorAccountVO>();
				for (IndicatorEvaluationView input : find) {

					String indicatorValue = input.getIndicatorValue() == null ? "" : nf.format(input
							.getIndicatorValue());
					String editValue = input.getEditValue() == null ? "" : nf.format(input.getEditValue());
					IndicatorAccountVO inputVO = BeanMapper.map(input, IndicatorAccountVO.class);
					inputVO.setIndicatorName(input.getName());
					inputVO.setIndicatorUnit(input.getUnit());
					inputVO.setIndicatorValue(indicatorValue);
					inputVO.setEditValue(editValue);
					list.add(inputVO);

				}

				return new Result(page.getPageNumber(), page.getPageSize(), count, list, new ArrayList<Event>());
			}
		}
		return null;
	}

	@Override
	public void removeIndicator(String[] ids) {
		if (ids != null && ids.length > 0) {
			Filter filter = new Filter();
			filter.addRule(new FilterRule(FilterConstant.COLLECTION, "id", ids));

			List<IndicatorData> indicators = indicatorDAO.find(IndicatorData.class, filter, null);
			indicatorDAO.delete(indicators);
		}
	}

	@Override
	public Double checkIndicator(int id, String areaId, int timeTYpe, int year) {
		IndicatorInput indicatorInput = indicatorInputDAO.get(IndicatorInput.class, id);
		if (indicatorInput != null) {
			Filter filter = new Filter();
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType", timeTYpe));
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", year));
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "indicatorId", id));
			// 省市指标值不同
			if (0 == indicatorInput.getDataIsUnique()) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", areaId));
			} else {
				filter.addRule(new FilterRule(FilterConstant.GET_NULL, "areaId", null));
			}
			List<IndicatorData> indicators = indicatorDAO.find(IndicatorData.class, filter, null);
			if (indicators != null && indicators.size() > 0) {
				IndicatorData indicatorData = indicators.get(0);
				return indicatorData.getIndicatorValue();
			}
		}
		return null;
	}

	@Override
	public Dustry checkGDPCodeExists(String name, int year) {
		Condition cnd = Cnd.where(Exps.eq("name", name)).and(Exps.eq("year", year));
		Dustry dustry = dao.fetch(Dustry.class, cnd);
		return dustry;
	}

	@Override
	public IndicatorData checkIndicatorExists(int id, String areaId, Integer timeType, Integer yearType, int year,
			Integer quarter) {
		IndicatorInput indicatorInput = indicatorInputDAO.get(IndicatorInput.class, id);
		if (indicatorInput != null) {
			Filter filter = new Filter();
			if (timeType != null && timeType != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType", timeType));
			}
			if (yearType != null && yearType != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "yearTime", yearType));
			}
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", year));
			if (quarter != null && quarter != 0) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "quarter", quarter));
			}
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "indicatorId", id));
			// 省市指标值不同
			if (0 == indicatorInput.getDataIsUnique()) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", areaId));
			} else {
				filter.addRule(new FilterRule(FilterConstant.GET_NULL, "areaId", null));
			}
			List<IndicatorData> indicators = indicatorDAO.find(IndicatorData.class, filter, null);
			if (indicators != null && indicators.size() > 0) {
				IndicatorData indicatorData = indicators.get(0);
				return indicatorData;
			}
		}
		return null;
	}

	@Override
	public boolean checkUpdateIndicator(String id, String areaId) {
		IndicatorData data = indicatorDAO.get(IndicatorData.class, id);
		// 创建者
		User createPre = data.getCreatePre();
		// 是省录入的
		if ("0".equals(createPre.getArea().getParentId())) {

			Area area = areaDAO.get(Area.class, areaId);
			// 市不能修改
			if (!"0".equals(area.getParentId())) {
				return false;
			}
		}
		return true;
	}

	@Override
	public String updateIndicator(IndicatorDataVO indiVo) {
		IndicatorData indicator = indicatorDAO.get(IndicatorData.class, indiVo.getId());
		User user = new User();
		String createPre = indiVo.getCreatePreId();
		user.setId(createPre);
		indicator.setCreatePre(user);
		indicator.setIndicatorValue(indiVo.getIndicatorValue());
		indicatorDAO.update(indicator);
		return indicator.getId();
	}

	/**
	 * 从核算指标中取值
	 *
	 * @param indicatorAccounting 当前进行计算的核算指标
	 * @param reference 当前进行计算的核算指标的引用指标
	 * @param indicatorAccountVO 核算条件
	 * @return
	 */
	private Double getValueFromIndicatorAccounting(
			cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting indicatorAccounting,
			IndicatorAccountingReference reference, IndicatorAccountVO indicatorAccountVO) {
		Double result;
		cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting referenceIndicatorAccounting = indicatorSystemDao
				.fetch(cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting.class,
						reference.getReferenceIndicatorId());

		String areaId = indicatorAccountVO.getAreaId();
		// 判断引用的核算指标是否与当前进行运算的核算指标级别一致
		if (!indicatorAccounting.getSection().equals(referenceIndicatorAccounting.getSection())) {
			// 省级
			if (referenceIndicatorAccounting.getSection() == 1) {
				areaId = provinceAreaId;
			} else if (referenceIndicatorAccounting.getSection() == 2) {
				// 市级
				areaId = StringUtils.left(areaId, 4) + "00000000000";
			}
		}

		Criteria cri = calcuIndicatorAccoutService.getIndicatorAccountingDataCri(indicatorAccountVO,
				referenceIndicatorAccounting, areaId, indicatorAccountVO.getYear());
		//
		Record record = dao.fetch("tbl_indicator_account", cri);
		if (record == null) {
			result = calcuIndicatorAccoutService.calculateIndicator(indicatorAccountVO, referenceIndicatorAccounting);
		} else {
			result = Castors.me().castTo(record.get("edit_value"), Double.class);
		}
		return calcuIndicatorAccoutService.reducingResultMagnitude(result, reference.getReferenceIndicatorType(),
				reference.getReferenceIndicatorId());
	}

	/**
	 * 计算单指标核算值
	 * @param accountingResultId
	 */
	@Override
	public void calculateSingleIndicator(String accountingResultId) {
		calcuIndicatorAccoutService.calculateSingleIndicator(accountingResultId);
	}

	/**
	 * 更新指标时同时更新对比值
	 */
	public IndicatorAccount editAccount(IndicatorAccountVO indiVo, IndicatorAccount indicator, Double valueOf) {
		Filter filter = new Filter();
		if (indicator.getTimeType() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType", indicator.getTimeType()));
		} else {
			filter.addRule(new FilterRule(FilterConstant.GET_NULL, "timeType", indicator.getTimeType()));
		}
		if (indicator.getYearTime() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "yearTime", indicator.getYearTime()));
		} else {
			filter.addRule(new FilterRule(FilterConstant.GET_NULL, "yearTime", indicator.getYearTime()));
		}
		if (indicator.getYear() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", indicator.getYear() - 1));
		} else {
			filter.addRule(new FilterRule(FilterConstant.GET_NULL, "year", indicator.getYear() - 1));
		}
		if (indicator.getQuarter() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "quarter", indicator.getQuarter()));
		} else {
			filter.addRule(new FilterRule(FilterConstant.GET_NULL, "quarter", indicator.getQuarter()));
		}
		if (indicator.getIndicatorId() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "indicatorId", indicator.getIndicatorId()));
		} else {
			filter.addRule(new FilterRule(FilterConstant.GET_NULL, "indicatorId", indicator.getIndicatorId()));
		}
		if (indicator.getAreaId() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", indicator.getAreaId()));
		} else {
			filter.addRule(new FilterRule(FilterConstant.GET_NULL, "areaId", indicator.getAreaId()));
		}
		List<IndicatorAccount> list = indicatorAccountDAO.find(IndicatorAccount.class, filter, null);
		if (list != null && list.size() > 0) {
			IndicatorAccount indicatorLast = list.get(0);
			Double editValue = indicatorLast.getEditValue();
			if (editValue == null || editValue.isNaN() || editValue.isInfinite()) {

			} else {
				indicator.setEditValueGrowthRate(editValue == 0 ? 0 : (valueOf - editValue) / editValue * 100);
			}
		}
		Condition cnd = Cnd.where("id", "=", indicator.getIndicatorId());
		List<cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting> indicatorAccountings = indicatorSystemDao
				.query(cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting.class, cnd);
		if (indicatorAccountings != null && indicatorAccountings.size() > 0) {
			cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting indicatorAccounting = indicatorAccountings
					.get(0);
			if (indicatorAccounting.getEqualIndicatorInputId() != null) {

				// 年鉴偏离度
				Double yearBookIndicatorValue = calcuIndicatorAccoutService.getYearBookIndicatorValue(indiVo,
						indicatorAccounting);
				if (yearBookIndicatorValue == null || yearBookIndicatorValue.isNaN()
						|| yearBookIndicatorValue.isInfinite()) {

				} else {
					indicator.setEvYearBookGrowthRate(yearBookIndicatorValue == 0 ? 0
							: (valueOf - yearBookIndicatorValue) / yearBookIndicatorValue * 100);
				}
				// 去年年鉴增加速度
				IndicatorAccountVO lastIndicatorAccount = BeanMapper.map(indiVo, IndicatorAccountVO.class);
				lastIndicatorAccount.setYear(indiVo.getYear() - 1);
				Double lastYearBookIndicatorValue = calcuIndicatorAccoutService.getYearBookIndicatorValue(
						lastIndicatorAccount, indicatorAccounting);
				if (lastYearBookIndicatorValue == null || lastYearBookIndicatorValue.isNaN()
						|| lastYearBookIndicatorValue.isInfinite()) {

				} else {
					indicator.setEvLastYearBookGrowthRate(lastYearBookIndicatorValue == 0 ? 0
							: (valueOf - lastYearBookIndicatorValue) / lastYearBookIndicatorValue * 100);
				}
			}
		}
		return indicator;
	}

	private void queryAccount(IndicatorAccount indicator, IndicatorAccountVO indiVo) {
		List<IndicatorAccountingReference> indicatorAccountingReferences = calcuIndicatorAccoutService
				.getIndicatorAccountingRefs(indicator.getIndicatorId(), indiVo.getAreaId());
		cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting calcuIndicator = indicatorSystemDao.fetch(
				cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting.class, indicator.getIndicatorId());
		for (IndicatorAccountingReference iar : indicatorAccountingReferences) {
			Double indicatorResult = null;
			cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting indicatorAccounting = indicatorSystemDao
					.fetch(cn.com.generaldata.jsme_indicator_system.entity.IndicatorAccounting.class,
							iar.getIndicatorAccountingId());

			if (indicatorAccounting.getPriority() > calcuIndicator.getPriority()) {
				return;
			}

			Filter filter = new Filter();
			if (indicator.getTimeType() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType", indicator.getTimeType()));
			}
			if (indicator.getYearTime() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "yearTime", indicator.getYearTime()));
			}
			if (indicator.getYear() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", indicator.getYear()));
			}
			if (indicator.getQuarter() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "quarter", indicator.getQuarter()));
			}
			if (indicator.getIndicatorId() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "indicatorId", iar.getIndicatorAccountingId()));
			}
			if (indicator.getAreaId() != null) {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", indicator.getAreaId()));
			}

			List<IndicatorAccount> list = indicatorAccountDAO.find(IndicatorAccount.class, filter, null);
			if (list != null && list.size() > 0) {
				IndicatorAccount indicatorSum = list.get(0);
				// IndicatorAccount indicatorSum = indicatorAccountDAO.get(
				// IndicatorAccount.class, iar.getIndicatorAccountingId().toString());
				Condition cnd2 = Cnd.where(Exps.eq("indicatorAccountingId", iar.getIndicatorAccountingId()));
				List<IndicatorAccountingReference> iarList = indicatorSystemDao.query(
						IndicatorAccountingReference.class, cnd2);
				Context ctx = Lang.context();
				// 每个汇总指标所引用的基础指标关系
				for (IndicatorAccountingReference reference : iarList) {
					Double referenceIndicatorVal = getValueFromIndicatorAccounting(indicatorAccounting, reference,
							indiVo);
					ctx.set(reference.getFormulaName(), referenceIndicatorVal);
				}
				try {
					Object evalResult = El.eval(ctx, indicatorAccounting.getFormula());
					indicatorResult = Castors.me().castTo(evalResult, Double.class);
				} catch (Exception e) {
					log.error("运算核算公式发生错误，核算指标信息 : {}", Json.toJson(indicatorAccounting));
					log.error("", e);
					indicatorResult = Double.valueOf("0");
				}

				Double reducingIndicatorResult = indicatorResult / indicatorAccounting.getUnitMagnitude();

				indicatorSum = editAccount(indiVo, indicatorSum, reducingIndicatorResult);
				indicatorSum.setEditValue(reducingIndicatorResult);
				indicatorAccountDAO.update(indicatorSum);
				queryAccount(indicatorSum, indiVo);
			}
		}
	}

	/**
	 * 更新指标
	 */
	@Override
	public String updateIndicatorAccount(IndicatorAccountVO indiVo) {
		IndicatorAccount indicator = indicatorAccountDAO.get(IndicatorAccount.class, indiVo.getId());

		Double valueOf = Double.valueOf(indiVo.getEditValue());

		indicator = editAccount(indiVo, indicator, valueOf);
		indicator.setEditValue(valueOf);
		indicatorAccountDAO.update(indicator);
		queryAccount(indicator, indiVo);
		return indicator.getId();
	}
	
	/**
	 * 查询指标值
	 */
	@Override
	public List<IndicatorAccount> queryIndicatorAccount(IndicatorAccountVO indiVo) {
		
		Filter filter = new Filter();
		if (indiVo.getTimeType() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType", indiVo.getTimeType()));
		}
		if (indiVo.getYearTime() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "yearTime", indiVo.getYearTime()));
		}
		if (indiVo.getYear() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", indiVo.getYear()));
		}
		if (indiVo.getQuarter() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "quarter", indiVo.getQuarter()));
		}
		if (indiVo.getIndicatorId() != null) {
			
			String sql = "select * from t_indicator_accounting t where t.name in (select t.name from T_INDICATOR_ACCOUNTING t where t.id = '" + indiVo.getIndicatorId() + "')";
			IndicatorAccounting vo = indicatorAccountingDAO.get(IndicatorAccounting.class, indiVo.getIndicatorId());
			
			String hql = "from IndicatorAccounting as ia where ia.name = '" + vo.getName() + "'";
			List<IndicatorAccounting> list = indicatorAccountingDAO.findByHql(hql, null);
			
			Integer[] ids = new Integer[list.size()];
			IndicatorAccounting indicatorAt = null;
			for (int i=0 ; i<list.size(); i++){
				indicatorAt = list.get(i);
				ids[i] = indicatorAt.getId();
			}
			
			filter.addRule(new FilterRule(FilterConstant.COLLECTION, "indicatorId", ids));
		}
		if (indiVo.getAreaId() != null) {
			filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", indiVo.getAreaId()));
		}

		List<IndicatorAccount> list = indicatorAccountDAO.find(IndicatorAccount.class, filter, null);
		return list;
	}

	@Override
	public void indicatorAccount(IndicatorAccountVO vo) {
		calcuIndicatorAccoutService.calculate(vo);
	}

	@Override
	public void indicatorEvaluation(IndicatorAccountVO vo, int level) {
		indicatorEvaluationService.calculate(vo);
	}

	@Override
	public String importConsumpValue(File file, String uid) {
		Workbook wb = null;
		// int rowNum = 0;
		try {
			wb = new HSSFWorkbook(new FileInputStream(file));
			// wb = WorkbookFactory.create(file);
			// 生成一个字体
			Font font = wb.createFont();
			font.setColor(HSSFColor.RED.index);// HSSFColor.VIOLET.index //字体颜色
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {

				boolean error = false;
				String title = sheet.getRow(0).getCell(0).getStringCellValue();
				if ("直接消耗系数导入模板".equals(title)) {
					org.apache.poi.ss.usermodel.Row yearRow = sheet.getRow(1);
					// 年份
					Cell cellYear = yearRow.getCell(0);

					Integer yearValue = 0;
					if (cellYear != null) {
						CellStyle year = cellYear.getCellStyle();
						int cellStyle = cellYear.getCellType();
						String yearString = "";
						if (cellStyle == 0) {
							DecimalFormat df = new DecimalFormat("0");
							double numericCellValue = cellYear.getNumericCellValue();
							yearString = df.format(numericCellValue);
						} else if (cellStyle == 1) {
							yearString = cellYear.getStringCellValue();
						}
						yearValue = Integer.valueOf(yearString);
					} else {
						error = true;
						CellStyle cellStyle = yearRow.getCell(0).getCellStyle();
						cellStyle.setFont(font);
					}
					SqlTemplate sqlTemplate = new SqlTemplate(dao);
					String sql = "select MAX(year) from tbl_dustry where year<=" + yearValue;
					Integer year = sqlTemplate.queryForInt(sql, null);
					Condition cnd = Cnd.where(Exps.eq("year", year)).asc("code");
					List<Dustry> list = dao.query(Dustry.class, cnd);
					List<IndicatorConsumpt> icList = new ArrayList<IndicatorConsumpt>();
					for (int i = 2; i < list.size() + 2; i++) {
						// 自身产业
						Dustry dustry = list.get(i - 2);
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
						for (int j = 1; j < list.size() + 1; j++) {
							// 目标产业
							Dustry dustry2 = list.get(j - 1);
							IndicatorConsumpt ic = new IndicatorConsumpt();
							ic.setId(R.UU64());
							ic.setDustryId(dustry.getId());
							ic.setForeigeId(dustry2.getId());
							ic.setYear(yearValue);
							Cell cellValue = row.getCell(j);
							if (cellValue != null) {
								int cellStyle = cellValue.getCellType();
								double numericCellValue = 0d;
								if (cellStyle == 0) {
									numericCellValue = cellValue.getNumericCellValue();
								} else if (cellStyle == 1) {
									try {
										numericCellValue = Double.valueOf(cellValue.getStringCellValue());
									} catch (NumberFormatException e) {
										// TODO Auto-generated catch block
										error = true;
										CellStyle valueStyle = cellValue.getCellStyle();
										valueStyle.setFont(font);
									}
								}
								ic.setConsumptValue(numericCellValue);
							} else {
								ic.setConsumptValue(0d);
							}
							icList.add(ic);
						}
					}

					if (!error) {
						if (icList != null && icList.size() > 0) {
							Condition cnd2 = Cnd.where(Exps.eq("year", yearValue));
							dao.clear(IndicatorConsumpt.class, cnd2);
							dao.insert(icList);
						}
						file.delete();
						return "导入成功";
					} else {
						FileOutputStream out = new FileOutputStream(file);
						wb.write(out);
						out.close();
						return file.getName();
					}

				} else {
					return "导入的模板错误";
				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("IO出错", e);
		}
		return "errorTitle";
	}

	@Override
	public String importBaseValue(File file, String uid) {
		Workbook wb = null;
		// int rowNum = 0;
		try {
			wb = new HSSFWorkbook(new FileInputStream(file));
			// wb = WorkbookFactory.create(file);
			// 生成一个字体
			Font font = wb.createFont();
			font.setColor(HSSFColor.RED.index);// HSSFColor.VIOLET.index //字体颜色
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {

				boolean error = false;
				String title = sheet.getRow(0).getCell(0).getStringCellValue();
				if ("基本流量表导入模板".equals(title)) {
					org.apache.poi.ss.usermodel.Row yearRow = sheet.getRow(1);
					// 年份
					Cell cellYear = yearRow.getCell(0);

					Integer yearValue = 0;
					if (cellYear != null) {
						CellStyle year = cellYear.getCellStyle();
						int cellStyle = cellYear.getCellType();
						String yearString = "";
						if (cellStyle == 0) {
							DecimalFormat df = new DecimalFormat("0");
							double numericCellValue = cellYear.getNumericCellValue();
							yearString = df.format(numericCellValue);
						} else if (cellStyle == 1) {
							yearString = cellYear.getStringCellValue();
						}
						yearValue = Integer.valueOf(yearString);
					} else {
						error = true;
						CellStyle cellStyle = yearRow.getCell(0).getCellStyle();
						cellStyle.setFont(font);
					}
					SqlTemplate sqlTemplate = new SqlTemplate(dao);
					String sql = "select MAX(year) from tbl_dustry where year<=" + yearValue;
					Integer year = sqlTemplate.queryForInt(sql, null);
					Condition cnd = Cnd.where(Exps.eq("year", year)).asc("code");
					List<Dustry> list = dao.query(Dustry.class, cnd);
					List<IndicatorProduction> icList = new ArrayList<IndicatorProduction>();
					for (int i = 2; i < list.size() + 2; i++) {
						// 自身产业
						Dustry dustry = list.get(i - 2);
						IndicatorProduction ic = new IndicatorProduction();
						ic.setId(R.UU64());
						ic.setDustryId(dustry.getId());
						ic.setYear(yearValue);
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
						// 最终消费支出
						Cell expenditureValue = row.getCell(1);
						// 资本形成总额
						Cell grossValue = row.getCell(2);
						// 出口
						Cell exportValue = row.getCell(3);
						if (expenditureValue != null) {
							int cellStyle = expenditureValue.getCellType();
							double numericCellValue = 0d;
							if (cellStyle == 0) {
								numericCellValue = expenditureValue.getNumericCellValue();
							} else if (cellStyle == 1) {
								try {
									numericCellValue = Double.valueOf(expenditureValue.getStringCellValue());
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
									error = true;
									CellStyle valueStyle = expenditureValue.getCellStyle();
									valueStyle.setFont(font);
								}
							}
							ic.setExpenditure(numericCellValue);
						} else {
							ic.setExpenditure(0d);
						}
						if (grossValue != null) {
							int cellStyle = grossValue.getCellType();
							double numericCellValue = 0d;
							if (cellStyle == 0) {
								numericCellValue = grossValue.getNumericCellValue();
							} else if (cellStyle == 1) {
								try {
									numericCellValue = Double.valueOf(grossValue.getStringCellValue());
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
									error = true;
									CellStyle valueStyle = grossValue.getCellStyle();
									valueStyle.setFont(font);
								}
							}
							ic.setGross(numericCellValue);
						} else {
							ic.setGross(0d);
						}
						if (exportValue != null) {
							int cellStyle = exportValue.getCellType();
							double numericCellValue = 0d;
							if (cellStyle == 0) {
								numericCellValue = exportValue.getNumericCellValue();
							} else if (cellStyle == 1) {
								try {
									numericCellValue = Double.valueOf(exportValue.getStringCellValue());
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
									error = true;
									CellStyle valueStyle = exportValue.getCellStyle();
									valueStyle.setFont(font);
								}
							}
							ic.setExport(numericCellValue);
						} else {
							ic.setExport(0d);
						}
						icList.add(ic);
					}

					if (!error) {
						if (icList != null && icList.size() > 0) {
							Condition cnd2 = Cnd.where(Exps.eq("year", yearValue));
							dao.clear(IndicatorProduction.class, cnd2);
							dao.insert(icList);
						}
						file.delete();
						return "导入成功";
					} else {
						FileOutputStream out = new FileOutputStream(file);
						wb.write(out);
						out.close();
						return file.getName();
					}

				} else {
					return "导入的模板错误";
				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("IO出错", e);
		}
		return "errorTitle";
	}

	@Override
	public String importGDPCode(File file, String uid) {
		Workbook wb = null;
		// int rowNum = 0;
		try {
			wb = new HSSFWorkbook(new FileInputStream(file));
			// wb = WorkbookFactory.create(file);
			// 生成一个字体
			Font font = wb.createFont();
			font.setColor(HSSFColor.RED.index);// HSSFColor.VIOLET.index //字体颜色
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {

				boolean error = false;
				String title = sheet.getRow(0).getCell(0).getStringCellValue();
				if ("国民经济代码导入模板".equals(title)) {
					org.apache.poi.ss.usermodel.Row yearRow = sheet.getRow(1);
					// 年份
					Cell cellYear = yearRow.getCell(1);

					Integer yearValue = 0;
					if (cellYear != null) {
						CellStyle year = cellYear.getCellStyle();
						int cellStyle = cellYear.getCellType();
						String yearString = "";
						if (cellStyle == 0) {
							DecimalFormat df = new DecimalFormat("0");
							double numericCellValue = cellYear.getNumericCellValue();
							yearString = df.format(numericCellValue);
						} else if (cellStyle == 1) {
							yearString = cellYear.getStringCellValue();
						}
						yearValue = Integer.valueOf(yearString);
					} else {
						error = true;
						CellStyle cellStyle = yearRow.getCell(0).getCellStyle();
						cellStyle.setFont(font);
					}
					List<Dustry> list = new ArrayList<Dustry>();
					for (int i = 3; i <= sheet.getLastRowNum(); i++) {
						Dustry indicatorData = new Dustry();

						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);

						// 名称
						Cell cellIndicator = row.getCell(0);
						// 代码
						Cell cellValue = row.getCell(1);
						// 需要计算
						Cell cellEnable = row.getCell(2);
						// 错误
						Cell cellError = row.getCell(3);
						if (cellError == null) {
							cellError = row.createCell(3);
						}
						if (cellValue != null) {
							CellStyle value = cellValue.getCellStyle();
							int cellStyle = cellValue.getCellType();
							String code = "";
							if (cellStyle == 0) {
								DecimalFormat df = new DecimalFormat("0");
								double numericCellValue = cellValue.getNumericCellValue();
								code = df.format(numericCellValue);
							} else if (cellStyle == 1) {
								code = cellValue.getStringCellValue();
							}
							indicatorData.setCode(code);
						} else {
							continue;
							// error = true;
							// cellError.setCellValue("指标值不能为空");
							// CellStyle cellStyle = cellError.getCellStyle();
							// cellStyle.setFont(font);
						}
						if (cellIndicator != null) {
							CellStyle indicator = cellIndicator.getCellStyle();
							indicatorData.setName(cellIndicator.getStringCellValue());

						} else {
							continue;
						}
						if (cellEnable != null) {
							String enable = cellEnable.getStringCellValue();
							if ("是".equals(enable)) {
								indicatorData.setEnable(1);
							} else {
								indicatorData.setEnable(0);
							}
						}
						indicatorData.setYear(yearValue);
						list.add(indicatorData);
					}
					if (!error) {
						for (Dustry data : list) {
							Dustry exists = checkGDPCodeExists(data.getName(), data.getYear());
							if (exists != null) {
								exists.setCode(data.getCode());
								exists.setEnable(data.getEnable());
								dao.update(exists);
							} else {
								data.setId(R.UU64());
								dao.insert(data);
							}
						}
						file.delete();
						return "导入成功";
					} else {
						FileOutputStream out = new FileOutputStream(file);
						wb.write(out);
						out.close();
						return file.getName();
					}

				} else {
					return "导入的模板错误";
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("IO出错", e);
		}
		return "errorTitle";
	}

	@Override
	public String importIndicator(File file, String uid) {
		Workbook wb = null;
		// int rowNum = 0;
		try {
			wb = new HSSFWorkbook(new FileInputStream(file));
			// wb = WorkbookFactory.create(file);
			// 生成一个字体
			Font font = wb.createFont();
			font.setColor(HSSFColor.RED.index);// HSSFColor.VIOLET.index //字体颜色
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {
				int startYear = indicatorSystemSqlTemplate.queryForInt(
						"SELECT to_char(MIN(start_date), 'yyyy') FROM tbl_report_version ", null);
				int endYear = indicatorSystemSqlTemplate.queryForInt("SELECT to_char(MAX(end_date), 'yyyy') FROM tbl_report_version ",
						null);
				boolean error = false;
				String title = sheet.getRow(0).getCell(0).getStringCellValue();
				if ("指标数据导入模板".equals(title)) {
					org.apache.poi.ss.usermodel.Row areaRow = sheet.getRow(1);
					org.apache.poi.ss.usermodel.Row typeRow = sheet.getRow(2);
					org.apache.poi.ss.usermodel.Row yearRow = sheet.getRow(3);
					org.apache.poi.ss.usermodel.Row quarterRow = sheet.getRow(4);
					// 地区
					Cell cellArea = areaRow.getCell(1);
					// 频率
					Cell cellType = typeRow.getCell(1);
					// 年份
					Cell cellYear = yearRow.getCell(1);
					// 季度
					Cell cellQuarter = quarterRow.getCell(1);
					String area = "";
					if (cellArea != null) {
						area = cellArea.getStringCellValue();

						if (area != null && !"".equals(area)) {
							area = (String) areamap.get(area);
						}
					}
					Integer yearValue = 0;
					if (cellYear != null) {
						CellStyle year = cellYear.getCellStyle();
						int cellStyle = cellYear.getCellType();
						String yearString = "";
						if (cellStyle == 0) {
							DecimalFormat df = new DecimalFormat("0");
							double numericCellValue = cellYear.getNumericCellValue();
							yearString = df.format(numericCellValue);
						} else if (cellStyle == 1) {
							yearString = cellYear.getStringCellValue();
						}
						try {
							yearValue = Integer.valueOf(yearString);

							if (yearValue == 0) {
								return "请填写指标年份";
							}

							if (yearValue < startYear || yearValue > endYear) {
								error = true;
								year.setFont(font);
								// cellYear.setCellStyle(year);
							}
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
							error = true;
							year.setFont(font);
							// cellYear.setCellStyle(year);
						}
					} else {
						error = true;
						CellStyle cellStyle = yearRow.getCell(0).getCellStyle();
						cellStyle.setFont(font);
					}
					Integer timeValue = 0;
					Integer yearType = 0;
					Integer quarter = 0;
					if (cellType != null) {
						String timeType = cellType.getStringCellValue();
						if (timeType != null && !"".equals(timeType)) {
							if ("年报".equals(timeType)) {
								timeValue = 1;
								yearType = 2;
							} else if ("半年报".equals(timeType)) {
								timeValue = 1;
								yearType = 1;
							} else if ("季报".equals(timeType)) {
								timeValue = 2;
								if (cellQuarter != null) {
									String quarterName = cellQuarter.getStringCellValue();
									if (quarterName != null && !"".equals(quarterName)) {
										if ("第一季度".equals(quarterName)) {
											quarter = 1;
										} else if ("第二季度".equals(quarterName)) {
											quarter = 2;
										} else if ("第三季度".equals(quarterName)) {
											quarter = 3;
										} else if ("第四季度".equals(quarterName)) {
											quarter = 4;
										}
									} else {
										error = true;
										CellStyle cellStyle = quarterRow.getCell(0).getCellStyle();
										cellStyle.setFont(font);
									}
								} else {
									error = true;
									CellStyle cellStyle = quarterRow.getCell(0).getCellStyle();
									cellStyle.setFont(font);
								}
							}
						} else {
							error = true;
							CellStyle cellStyle = typeRow.getCell(0).getCellStyle();
							cellStyle.setFont(font);
						}
					} else {
						error = true;
						CellStyle cellStyle = typeRow.getCell(0).getCellStyle();
						cellStyle.setFont(font);
					}
					List<IndicatorData> list = new ArrayList<IndicatorData>();
					for (int i = 6; i <= sheet.getLastRowNum(); i++) {
						IndicatorData indicatorData = new IndicatorData();
						User user = new User();
						user.setId(uid);
						indicatorData.setCreatePre(user);

						indicatorData.setTimeType(timeValue);
						if (area != null && !"".equals(area)) {
							indicatorData.setAreaId(area);
						}
						indicatorData.setYear(yearValue);
						indicatorData.setYearTime(yearType);
						indicatorData.setQuarter(quarter);
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);

						// 指标
						Cell cellIndicator = row.getCell(1);
						// 指标值
						Cell cellValue = row.getCell(3);
						Cell cellError = row.getCell(4);
						if (cellError == null) {
							cellError = row.createCell(4);
						}
						if (cellValue != null) {
							CellStyle value = cellValue.getCellStyle();
							int cellStyle = cellValue.getCellType();
							String yearString = "";
							if (cellStyle == 0) {
								DecimalFormat nf = new DecimalFormat();
								nf.setMinimumFractionDigits(0);
								nf.setMaximumFractionDigits(15);
								double numericCellValue = cellValue.getNumericCellValue();
								yearString = nf.format(numericCellValue);
							} else if (cellStyle == 1) {
								yearString = cellValue.getStringCellValue();
							}
							try {
								Double valueValue = Double.valueOf(yearString);
								indicatorData.setIndicatorValue(valueValue);
							} catch (NumberFormatException e) {
								// TODO Auto-generated catch block
								// error = true;
								// value.setFont(font);
								continue;
								// cellYear.setCellStyle(value);
							}
						} else {
							continue;
							// error = true;
							// cellError.setCellValue("指标值不能为空");
							// CellStyle cellStyle = cellError.getCellStyle();
							// cellStyle.setFont(font);
						}
						if (cellIndicator != null) {
							CellStyle indicator = cellIndicator.getCellStyle();
							Filter filter = new Filter();
							filter.addRule(new FilterRule(FilterConstant.EQUAL, "name", cellIndicator
									.getStringCellValue()));
							List<IndicatorInput> find = indicatorInputDAO.find(IndicatorInput.class, filter, null);
							if (find != null && find.size() > 0) {
								IndicatorInput indicatorInput = find.get(0);
								indicatorData.setIndicatorId(indicatorInput.getId());
								// if (!error) {
								// Double checkIndicator = checkIndicator(
								// indicatorInput.getId(),
								// indicatorData.getAreaId(), 1,
								// indicatorData.getYear());
								// if (checkIndicator != null) {
								// error = true;
								// indicator.setFont(font);
								// cellError.setCellValue("指标值已存在，请删除该行");
								// CellStyle cellStyle = cellError
								// .getCellStyle();
								// cellStyle.setFont(font);
								// }
								// }
							} else {
								continue;
								// error = true;
								// indicator.setFont(font);
								// cellIndicator.setCellStyle(indicator);
							}
						} else {
							continue;
							// error = true;
							// cellError.setCellValue("指标不能为空");
							// CellStyle cellStyle = cellError.getCellStyle();
							// cellStyle.setFont(font);
						}
						list.add(indicatorData);
					}
					if (!error) {
						for (IndicatorData data : list) {
							IndicatorInput indicatorInput = indicatorInputDAO.get(IndicatorInput.class,
									data.getIndicatorId());
							IndicatorData exists = checkIndicatorExists(data.getIndicatorId(), data.getAreaId(),
									data.getTimeType(), data.getYearTime(), data.getYear(), data.getQuarter());
							if (exists != null) {
								exists.setIndicatorValue(data.getIndicatorValue());
								indicatorDAO.saveOrUpdate(exists);
							} else {
								data.setId(R.UU64());
								if (0 != indicatorInput.getDataIsUnique())
									data.setAreaId(null);
								indicatorDAO.save(data);
							}
						}
						file.delete();
						return "导入成功";
					} else {
						FileOutputStream out = new FileOutputStream(file);
						wb.write(out);
						out.close();
						return file.getName();
					}

				} else {
					return "导入的模板错误";
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("IO出错", e);
		}
		return "errorTitle";
	}

	@Override
	public void downCoefficientTemple(OutputStream out, String year) {
		InputStream resourceAsStream = getClass().getResourceAsStream("/input-coefficient-template.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			// 江苏省与全国
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {
				SqlTemplate sqlTemplate = new SqlTemplate(dao);
				String sql = "select MAX(year) from tbl_dustry where year<=" + year;
				Integer yearValue = sqlTemplate.queryForInt(sql, null);
				Condition cnd = Cnd.where(Exps.eq("year", yearValue)).asc("code");
				List<Dustry> list = dao.query(Dustry.class, cnd);
				if (list != null && list.size() > 0) {
					for (int i = 2; i < list.size() + 2; i++) {
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
						if (row == null) {
							sheet.createRow(i);
							row = sheet.getRow(i);
						}
						row.createCell(0);
						Cell cellName = row.getCell(0);
						Dustry dustry = list.get(i - 2);
						cellName.setCellValue(dustry.getName());
					}
					org.apache.poi.ss.usermodel.Row row = sheet.getRow(1);
					for (int i = 1; i < list.size() + 1; i++) {
						Cell cell = row.getCell(i);
						if (cell == null) {
							cell = row.createCell(i);
						}
						Dustry dustry = list.get(i - 1);
						cell.setCellValue(dustry.getName());
					}
				}
				Row row = sheet.getRow(1);
				if (row == null) {
					sheet.createRow(1);
					row = sheet.getRow(1);
				}
				row.createCell(0);
				row.getCell(0).setCellValue(year);
			}
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public void downBasicFlowTemple(OutputStream out, String year) {
		InputStream resourceAsStream = getClass().getResourceAsStream("/input-basicflow-template.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			// 江苏省与全国
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {
				SqlTemplate sqlTemplate = new SqlTemplate(dao);
				String sql = "select MAX(year) from tbl_dustry where year<=" + year;
				Integer yearValue = sqlTemplate.queryForInt(sql, null);
				Condition cnd = Cnd.where(Exps.eq("year", yearValue)).asc("code");
				List<Dustry> list = dao.query(Dustry.class, cnd);
				if (list != null && list.size() > 0) {
					for (int i = 2; i < list.size() + 2; i++) {
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
						if (row == null) {
							sheet.createRow(i);
							row = sheet.getRow(i);
						}
						row.createCell(0);
						Cell cellName = row.getCell(0);
						Dustry dustry = list.get(i - 2);
						cellName.setCellValue(dustry.getName());
					}
				}
				Row row = sheet.getRow(1);
				if (row == null) {
					sheet.createRow(1);
					row = sheet.getRow(1);
				}
				row.createCell(0);
				row.getCell(0).setCellValue(year);
			}
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public void downGDPCodeTemple(OutputStream out, String year) {
		InputStream resourceAsStream = getClass().getResourceAsStream("/input-gdpCode-template.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			// 江苏省与全国
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {
				Condition cnd = Cnd.where(Exps.eq("year", Integer.valueOf(year))).asc("code");
				List<Dustry> list = dao.query(Dustry.class, cnd);
				if (list != null && list.size() > 0) {
					for (int i = 3; i < list.size() + 3; i++) {
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
						if (row == null) {
							sheet.createRow(i);
							row = sheet.getRow(i);
						}
						row.createCell(0);
						row.createCell(1);
						row.createCell(2);
						Cell cellName = row.getCell(0);
						Cell cellCode = row.getCell(1);
						Cell cellType = row.getCell(2);
						Dustry dustry = list.get(i - 3);
						cellName.setCellValue(dustry.getName());
						cellCode.setCellValue(dustry.getCode());
						if (dustry.getEnable() == 1) {
							cellType.setCellValue("是");
						}
					}
				}
				Row row = sheet.getRow(1);
				if (row == null) {
					sheet.createRow(1);
					row = sheet.getRow(1);
				}
				row.createCell(1);
				row.getCell(1).setCellValue(year);
			}
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public void downIndicatorTemple(OutputStream out, String type, String industry) {
		InputStream resourceAsStream = getClass().getResourceAsStream("/input-indicator-template.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			// 江苏省与全国
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {
				List<SystemCodedic> types = queryType();
				Integer valueType = Integer.valueOf(type);
				Filter filter = new Filter();
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "category", valueType));
				if (!StringUtils.isBlank(industry)) {
					filter.addRule(new FilterRule(FilterConstant.EQUAL, "industryCode", industry));
				}

				filter.setOrder(FilterConstant.ASC);
				filter.setSortBy("category");
				List<IndicatorInput> list = indicatorInputDAO.find(IndicatorInput.class, filter, null);
				if (list != null && list.size() > 0) {
					for (int i = 6; i < list.size() + 6; i++) {
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
						if (row == null) {
							sheet.createRow(i);
							row = sheet.getRow(i);
						}
						row.createCell(0);
						row.createCell(1);
						row.createCell(2);
						row.createCell(3);
						Cell cellName = row.getCell(1);
						Cell cellValue = row.getCell(3);
						Cell cellUnit = row.getCell(2);
						Cell cellType = row.getCell(0);
						IndicatorInput indicatorInput = list.get(i - 6);
						cellName.setCellValue(indicatorInput.getName());
						cellUnit.setCellValue(indicatorInput.getUnit());
						for (SystemCodedic sc : types) {
							if (sc.getCode().equals(type)) {
								cellType.setCellValue(sc.getCodeName());
							}
						}
						// 是这三种类型，就将最新一期的数值
						if (valueType == 1 || valueType == 2 || valueType == 3) {
							String hql = "select indicatorValue from IndicatorData where indicatorId="
									+ indicatorInput.getId()
									+ " and year=(select max(year) from IndicatorData where indicatorId="
									+ indicatorInput.getId() + ")";
							List data = indicatorDAO.findByHqlReal(hql, null);
							if (data != null && data.size() > 0) {
								cellValue.setCellValue(data.get(0).toString());
							}
						}
					}
				}
			}
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public void downUnitTemple(OutputStream out) {
		InputStream resourceAsStream = getClass().getResourceAsStream("/unit_temple.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public void downEnterpriseTemple(OutputStream out) {
		InputStream resourceAsStream = getClass().getResourceAsStream("/enterprise_temple.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public void unitProTemple(OutputStream out) {
		InputStream resourceAsStream = getClass().getResourceAsStream("/unitpro_temple.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			wb.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public void exportAccountExcel(OutputStream out, Integer year, Integer yearEnd) {
		DecimalFormat nf = new DecimalFormat();
		nf.setMinimumFractionDigits(2);
		nf.setMaximumFractionDigits(2);
		InputStream resourceAsStream = getClass().getResourceAsStream("/IndicatorAccount.xls");
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(resourceAsStream);
			Filter filter = new Filter();
			String[] areaIds = new String[] { "320000000000000", "320700000000000", "320900000000000",
					"320600000000000" };
			filter.addRule(new FilterRule(FilterConstant.COLLECTION, "areaId", areaIds));
			if (yearEnd != 0) {
				filter.addRule(new FilterRule(FilterConstant.GE, "year", year));
				filter.addRule(new FilterRule(FilterConstant.LE, "year", yearEnd));
			} else {
				filter.addRule(new FilterRule(FilterConstant.EQUAL, "year", year));
			}
			List<IndicatorAccount> list = indicatorAccountDAO.find(IndicatorAccount.class, filter, null);
			// 江苏省与全国
			Sheet sheet = wb.getSheetAt(0);
			if (sheet != null) {
				org.apache.poi.ss.usermodel.Row row0 = sheet.getRow(0);
				Cell shengName = row0.getCell(4);
				String stringSheng = shengName.getStringCellValue();
				Cell guoName = row0.getCell(5);
				String stringGuo = guoName.getStringCellValue();
				Cell valueName = row0.getCell(6);
				String stringValue = valueName.getStringCellValue();
				Cell indicatorName = row0.getCell(7);
				String stringIndicator = indicatorName.getStringCellValue();
				org.apache.poi.ss.usermodel.Row row1 = sheet.getRow(1);
				Cell firstName = row1.getCell(4);
				String stringFirst = firstName.getStringCellValue();
				Cell endName = row1.getCell(5);
				String stringEnd = endName.getStringCellValue();
				Cell growName = row1.getCell(7);
				String stringGrow = growName.getStringCellValue();
				shengName.setCellValue(year + "年" + stringSheng);
				guoName.setCellValue(year + "年" + stringGuo);
				valueName.setCellValue(year + "年" + stringValue);
				indicatorName.setCellValue(year + "年" + stringIndicator);
				if (yearEnd != 0) {
					for (int a = 1; a <= yearEnd - year; a++) {
						Cell createCell = row0.createCell(4 + 4 * a);
						createCell.setCellValue(year + a + "年" + stringSheng);
						Cell createCell2 = row0.createCell(5 + 4 * a);
						createCell2.setCellValue(year + a + "年" + stringGuo);
						Cell createCell3 = row0.createCell(6 + 4 * a);
						createCell3.setCellValue(year + a + "年" + stringValue);
						Cell createCell6 = row0.createCell(7 + 4 * a);
						createCell6.setCellValue(year + a + "年" + stringIndicator);
						Cell createCell4 = row1.createCell(4 + 4 * a);
						createCell4.setCellValue(stringFirst);
						Cell createCell5 = row1.createCell(5 + 4 * a);
						createCell5.setCellValue(stringEnd);
						Cell createCell7 = row1.createCell(7 + 4 * a);
						createCell7.setCellValue(stringGrow);
						for (int i = 2; i <= 37; i++) {
							org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
							for (int b = 1; b <= yearEnd - year; b++) {
								row.createCell(4 + 4 * b);
								row.createCell(5 + 4 * b);
								row.createCell(6 + 4 * b);
								row.createCell(7 + 4 * b);
							}
						}
					}
					for (int y = year; y <= yearEnd; y++) {
						for (int i = 2; i <= 37; i++) {
							org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
							Cell cellName = row.getCell(3);
							String stringCellValue = cellName.getStringCellValue();
							Cell cellValue = row.getCell(4 + 4 * (y - year));
							Cell growValue = row.getCell(7 + 4 * (y - year));
							Double sum = Double.valueOf("0");
							if (list != null && list.size() > 0) {
								for (IndicatorAccount account : list) {
									if (account.getYear() == y) {
										IndicatorAccounting accounting = indicatorAccountingDAO.get(
												IndicatorAccounting.class, account.getIndicatorId());
										String name = accounting.getName();
										Integer category = accounting.getMarineIndustryCategory();
										Integer attr = accounting.getMarineIndustryAttr();
										if (stringCellValue.indexOf(name) != -1) {
											if ("320000000000000".equals(account.getAreaId())) {
												cellValue.setCellValue(account.getEditValue());
												growValue.setCellValue(nf.format(account.getEditValueGrowthRate()));
												break;

											}
										}
									}
								}
							}
						}
						for (IndicatorAccount account : list) {
							if (account.getYear() == y) {
								IndicatorAccounting accounting = indicatorAccountingDAO.get(IndicatorAccounting.class,
										account.getIndicatorId());
								if ("320000000000000".equals(account.getAreaId())) {
									String name = accounting.getName();
									Double editValue = account.getEditValue();
									String indicatorValueGrowthRate = nf.format(account.getEditValueGrowthRate());
									if ("主要海洋产业增加值".equals(name)) {
										sheet.getRow(4).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(4).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
									if ("海洋科研教育管理服务业增加值".equals(name)) {
										sheet.getRow(17).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(17).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
									if ("海洋相关产业增加值".equals(name)) {
										sheet.getRow(28).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(28).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
									if ("海洋产业增加值".equals(name)) {
										sheet.getRow(3).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(3).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
									if ("海洋第一产业占比重".equals(name)) {
										sheet.getRow(35).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(35).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
									if ("海洋第二产业占比重".equals(name)) {
										sheet.getRow(36).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(36).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
									if ("海洋第三产业占比重".equals(name)) {
										sheet.getRow(37).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(37).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
									if ("海洋生产总值".equals(name)) {
										sheet.getRow(2).getCell(4 + 4 * (y - year)).setCellValue(editValue);
										sheet.getRow(2).getCell(7 + 4 * (y - year))
												.setCellValue(indicatorValueGrowthRate);
									}
								}
							}
						}
					}
				} else {
					for (int i = 2; i <= 37; i++) {
						org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
						Cell cellName = row.getCell(3);
						String stringCellValue = cellName.getStringCellValue();
						Cell cellValue = row.getCell(4);
						Cell growValue = row.getCell(7);
						Double sum = Double.valueOf("0");
						if (list != null && list.size() > 0) {
							for (IndicatorAccount account : list) {
								IndicatorAccounting accounting = indicatorAccountingDAO.get(IndicatorAccounting.class,
										account.getIndicatorId());
								String name = accounting.getName();
								Integer category = accounting.getMarineIndustryCategory();
								Integer attr = accounting.getMarineIndustryAttr();
								if (stringCellValue.indexOf(name) != -1) {
									if ("320000000000000".equals(account.getAreaId())) {
										cellValue.setCellValue(account.getEditValue());
										growValue.setCellValue(nf.format(account.getEditValueGrowthRate()));
										break;
									}
								}
							}
						}
					}
					for (IndicatorAccount account : list) {
						IndicatorAccounting accounting = indicatorAccountingDAO.get(IndicatorAccounting.class,
								account.getIndicatorId());
						if ("320000000000000".equals(account.getAreaId())) {
							String name = accounting.getName();
							Double editValue = account.getEditValue();
							String indicatorValueGrowthRate = nf.format(account.getEditValueGrowthRate());
							if ("主要海洋产业增加值".equals(name)) {
								sheet.getRow(4).getCell(4).setCellValue(editValue);
								sheet.getRow(4).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
							if ("海洋科研教育管理服务业增加值".equals(name)) {
								sheet.getRow(17).getCell(4).setCellValue(editValue);
								sheet.getRow(17).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
							if ("海洋相关产业增加值".equals(name)) {
								sheet.getRow(28).getCell(4).setCellValue(editValue);
								sheet.getRow(28).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
							if ("海洋产业增加值".equals(name)) {
								sheet.getRow(3).getCell(4).setCellValue(editValue);
								sheet.getRow(3).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
							if ("海洋第一产业占比重".equals(name)) {
								sheet.getRow(35).getCell(4).setCellValue(editValue);
								sheet.getRow(35).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
							if ("海洋第二产业占比重".equals(name)) {
								sheet.getRow(36).getCell(4).setCellValue(editValue);
								sheet.getRow(36).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
							if ("海洋第三产业占比重".equals(name)) {
								sheet.getRow(37).getCell(4).setCellValue(editValue);
								sheet.getRow(37).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
							if ("海洋生产总值".equals(name)) {
								sheet.getRow(2).getCell(4).setCellValue(editValue);
								sheet.getRow(2).getCell(7).setCellValue(indicatorValueGrowthRate);
							}
						}
					}
				}
			}
			// 江苏省与沿海三市
			Sheet sheet1 = wb.getSheetAt(1);
			if (sheet1 != null) {
				org.apache.poi.ss.usermodel.Row row0 = sheet1.getRow(0);
				Cell shengName = row0.getCell(4);
				String stringSheng = shengName.getStringCellValue();
				Cell shengIndicator = row0.getCell(5);
				String stringShengIndicator = shengIndicator.getStringCellValue();
				Cell sumName = row0.getCell(6);
				String stringSum = sumName.getStringCellValue();
				Cell lygName = row0.getCell(7);
				String stringLyg = lygName.getStringCellValue();
				Cell lygIndicator = row0.getCell(8);
				String stringLygIndicator = lygIndicator.getStringCellValue();
				Cell ycName = row0.getCell(9);
				String stringYc = ycName.getStringCellValue();
				Cell ycIndicator = row0.getCell(10);
				String stringYcIndicator = ycIndicator.getStringCellValue();
				Cell ntName = row0.getCell(11);
				String stringNt = ntName.getStringCellValue();
				Cell ntIndicator = row0.getCell(12);
				String stringNtIndicator = ntIndicator.getStringCellValue();
				org.apache.poi.ss.usermodel.Row row1 = sheet1.getRow(1);
				Cell firstName = row1.getCell(4);
				String stringFirst = firstName.getStringCellValue();
				Cell rateName = row1.getCell(5);
				String stringRate = rateName.getStringCellValue();
				shengName.setCellValue(year + "年" + stringSheng);
				sumName.setCellValue(year + "年" + stringSum);
				lygName.setCellValue(year + "年" + stringLyg);
				ntName.setCellValue(year + "年" + stringNt);
				ycName.setCellValue(year + "年" + stringYc);
				shengIndicator.setCellValue(year + "年" + stringShengIndicator);
				lygIndicator.setCellValue(year + "年" + stringLygIndicator);
				ycIndicator.setCellValue(year + "年" + stringYcIndicator);
				ntIndicator.setCellValue(year + "年" + stringNtIndicator);
//				if (yearEnd != 0) {
					for (int a = 1; a <= yearEnd - year; a++) {
						Cell createCell = row0.createCell(4 + 9 * a);
						createCell.setCellValue(year + a + "年" + stringSheng);
						Cell createCell2 = row0.createCell(5 + 9 * a);
						createCell2.setCellValue(year + a + "年" + stringShengIndicator);
						Cell createCell3 = row0.createCell(6 + 9 * a);
						createCell3.setCellValue(year + a + "年" + stringSum);
						Cell createCell5 = row0.createCell(7 + 9 * a);
						createCell5.setCellValue(year + a + "年" + stringLyg);
						Cell createCell6 = row0.createCell(8 + 9 * a);
						createCell6.setCellValue(year + a + "年" + stringLygIndicator);
						Cell createCell7 = row0.createCell(9 + 9 * a);
						createCell7.setCellValue(year + a + "年" + stringYc);
						Cell createCell8 = row0.createCell(10 + 9 * a);
						createCell8.setCellValue(year + a + "年" + stringYcIndicator);
						Cell createCell9 = row0.createCell(11 + 9 * a);
						createCell9.setCellValue(year + a + "年" + stringNt);
						Cell createCell10 = row0.createCell(12 + 9 * a);
						createCell10.setCellValue(year + a + "年" + stringNtIndicator);
						Cell createCell11 = row1.createCell(4 + 9 * a);
						createCell11.setCellValue(stringFirst);
						Cell createCell12 = row1.createCell(5 + 9 * a);
						createCell12.setCellValue(stringRate);
						Cell createCell13 = row1.createCell(6 + 9 * a);
						createCell13.setCellValue(stringFirst);
						Cell createCell14 = row1.createCell(7 + 9 * a);
						createCell14.setCellValue(stringFirst);
						Cell createCell15 = row1.createCell(8 + 9 * a);
						createCell15.setCellValue(stringRate);
						Cell createCell16 = row1.createCell(9 + 9 * a);
						createCell16.setCellValue(stringFirst);
						Cell createCell17 = row1.createCell(10 + 9 * a);
						createCell17.setCellValue(stringRate);
						Cell createCell18 = row1.createCell(11 + 9 * a);
						createCell18.setCellValue(stringFirst);
						Cell createCell19 = row1.createCell(12 + 9 * a);
						createCell19.setCellValue(stringRate);
						for (int i = 2; i <= 37; i++) {
							org.apache.poi.ss.usermodel.Row row = sheet1.getRow(i);
							for (int b = 1; b <= yearEnd - year; b++) {
								row.createCell(4 + 9 * b);
								row.createCell(5 + 9 * b);
								row.createCell(6 + 9 * b);
								row.createCell(7 + 9 * b);
								row.createCell(8 + 9 * b);
								row.createCell(9 + 9 * b);
								row.createCell(10 + 9 * b);
								row.createCell(11 + 9 * b);
								row.createCell(12 + 9 * b);
							}
						}
					}
					for (int y = year; y <= yearEnd; y++) {
						for (IndicatorAccount account : list) {
							if (account.getYear() == y) {
								IndicatorAccounting accounting = indicatorAccountingDAO.get(IndicatorAccounting.class,
										account.getIndicatorId());

								String name = accounting.getName();
								Double editValue = account.getEditValue();
								String editValueGrowthRate = nf.format(account.getEditValueGrowthRate());
								int i = 0;
								// 江苏
								if ("320000000000000".equals(account.getAreaId())) {
									i = 4 + 9 * (y - year);
								}
								// 盐城
								else if ("320900000000000".equals(account.getAreaId())) {
									i = 9 + 9 * (y - year);
								}
								// 南通
								else if ("320600000000000".equals(account.getAreaId())) {
									i = 11 + 9 * (y - year);
								}
								// 连云港
								else if ("320700000000000".equals(account.getAreaId())) {
									i = 7 + 9 * (y - year);
								}
								if (i != 0) {
									if ("主要海洋产业增加值".equals(name)) {
										sheet1.getRow(4).getCell(i).setCellValue(editValue);
										sheet1.getRow(4).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
									if ("海洋科研教育管理服务业增加值".equals(name)) {
										sheet1.getRow(17).getCell(i).setCellValue(editValue);
										sheet1.getRow(17).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
									if ("海洋相关产业增加值".equals(name)) {
										sheet1.getRow(28).getCell(i).setCellValue(editValue);
										sheet1.getRow(28).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
									if ("海洋产业增加值".equals(name)) {
										sheet1.getRow(3).getCell(i).setCellValue(editValue);
										sheet1.getRow(3).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
									if ("海洋第一产业占比重".equals(name)) {
										sheet1.getRow(35).getCell(i).setCellValue(editValue);
										sheet1.getRow(35).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
									if ("海洋第二产业占比重".equals(name)) {
										sheet1.getRow(36).getCell(i).setCellValue(editValue);
										sheet1.getRow(36).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
									if ("海洋第三产业占比重".equals(name)) {
										sheet1.getRow(37).getCell(i).setCellValue(editValue);
										sheet1.getRow(37).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
									if ("海洋生产总值".equals(name)) {
										sheet1.getRow(2).getCell(i).setCellValue(editValue);
										sheet1.getRow(2).getCell(i + 1).setCellValue(editValueGrowthRate);
									}
								}
							}
						}
						for (int i = 2; i <= 41; i++) {
							org.apache.poi.ss.usermodel.Row row = sheet1.getRow(i);
							Cell cellName = row.getCell(3);
							String stringCellValue = cellName.getStringCellValue();
							Cell cellJSValue = row.getCell(4 + 9 * (y - year));
							Cell cellJSIndicatorValue = row.getCell(5 + 9 * (y - year));
							Cell cellSumValue = row.getCell(6 + 9 * (y - year));
							Cell cellLYGValue = row.getCell(7 + 9 * (y - year));
							Cell cellLYGIndicatorValue = row.getCell(8 + 9 * (y - year));
							Cell cellYCValue = row.getCell(9 + 9 * (y - year));
							Cell cellYCIndicatorValue = row.getCell(10 + 9 * (y - year));
							Cell cellNTValue = row.getCell(11 + 9 * (y - year));
							Cell cellNTIndicatorValue = row.getCell(12 + 9 * (y - year));
							if (list != null && list.size() > 0) {
								for (IndicatorAccount account : list) {
									if (account.getYear() == y) {
										IndicatorAccounting accounting = indicatorAccountingDAO.get(
												IndicatorAccounting.class, account.getIndicatorId());
										String name = accounting.getName();
										if (stringCellValue.indexOf(name) != -1) {
											if ("320000000000000".equals(account.getAreaId())) {
												cellJSValue.setCellValue(account.getEditValue());
												cellJSIndicatorValue.setCellValue(nf.format(account
														.getEditValueGrowthRate()));
											} else if ("320900000000000".equals(account.getAreaId())) {
												cellYCValue.setCellValue(account.getEditValue());
												cellYCIndicatorValue.setCellValue(nf.format(account
														.getEditValueGrowthRate()));
											} else if ("320600000000000".equals(account.getAreaId())) {
												cellNTValue.setCellValue(account.getEditValue());
												cellNTIndicatorValue.setCellValue(nf.format(account
														.getEditValueGrowthRate()));
											} else if ("320700000000000".equals(account.getAreaId())) {
												cellLYGValue.setCellValue(account.getEditValue());
												cellLYGIndicatorValue.setCellValue(nf.format(account
														.getEditValueGrowthRate()));
											}
										}
									}
								}
							}
							if (cellLYGValue.getCellType() == Cell.CELL_TYPE_NUMERIC
									|| cellYCValue.getCellType() == Cell.CELL_TYPE_NUMERIC
									|| cellNTValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
								Double all = Double.valueOf("0");
								if (cellLYGValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
									all += Double.valueOf(cellLYGValue.getNumericCellValue());
								}
								if (cellYCValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
									all += Double.valueOf(cellYCValue.getNumericCellValue());
								}
								if (cellNTValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
									all += Double.valueOf(cellNTValue.getNumericCellValue());
								}
								cellSumValue.setCellValue(all);
							}
						}
					}
					
//				} else {
//					for (IndicatorAccount account : list) {
//						IndicatorAccounting accounting = indicatorAccountingDAO.get(IndicatorAccounting.class,
//								account.getIndicatorId());
//
//						String name = accounting.getName();
//						Double editValue = account.getEditValue();
//						String editValueGrowthRate = nf.format(account.getEditValueGrowthRate());
//						int i = 98;
//						// 江苏
//						if ("320000000000000".equals(account.getAreaId())) {
//							i = 4;
//						}
//						// 盐城
//						else if ("320900000000000".equals(account.getAreaId())) {
//							i = 9;
//						}
//						// 南通
//						else if ("320600000000000".equals(account.getAreaId())) {
//							i = 11;
//						}
//						// 连云港
//						else if ("320700000000000".equals(account.getAreaId())) {
//							i = 7;
//						}
//						if (i != 98) {
//							if ("主要海洋产业增加值".equals(name)) {
//								sheet1.getRow(4).getCell(i).setCellValue(editValue);
//								sheet1.getRow(4).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//							if ("海洋科研教育管理服务业增加值".equals(name)) {
//								sheet1.getRow(17).getCell(i).setCellValue(editValue);
//								sheet1.getRow(17).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//							if ("海洋相关产业增加值".equals(name)) {
//								sheet1.getRow(28).getCell(i).setCellValue(editValue);
//								sheet1.getRow(28).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//							if ("海洋产业增加值".equals(name)) {
//								sheet1.getRow(3).getCell(i).setCellValue(editValue);
//								sheet1.getRow(3).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//							if ("海洋第一产业占比重".equals(name)) {
//								sheet1.getRow(35).getCell(i).setCellValue(editValue);
//								sheet1.getRow(35).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//							if ("海洋第二产业占比重".equals(name)) {
//								sheet1.getRow(36).getCell(i).setCellValue(editValue);
//								sheet1.getRow(36).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//							if ("海洋第三产业占比重".equals(name)) {
//								sheet1.getRow(37).getCell(i).setCellValue(editValue);
//								sheet1.getRow(37).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//							if ("海洋生产总值".equals(name)) {
//								sheet1.getRow(2).getCell(i).setCellValue(editValue);
//								sheet1.getRow(2).getCell(i + 1).setCellValue(editValueGrowthRate);
//							}
//						}
//					}
//					for (int i = 2; i <= 37; i++) {
//						org.apache.poi.ss.usermodel.Row row = sheet1.getRow(i);
//						Cell cellName = row.getCell(3);
//						String stringCellValue = cellName.getStringCellValue();
//						Cell cellJSValue = row.getCell(4);
//						Cell cellJSIndicatorValue = row.getCell(5);
//						Cell cellSumValue = row.getCell(6);
//						Cell cellLYGValue = row.getCell(7);
//						Cell cellLYGIndicatorValue = row.getCell(8);
//						Cell cellYCValue = row.getCell(9);
//						Cell cellYCIndicatorValue = row.getCell(10);
//						Cell cellNTValue = row.getCell(11);
//						Cell cellNTIndicatorValue = row.getCell(12);
//						if (list != null && list.size() > 0) {
//							for (IndicatorAccount account : list) {
//								IndicatorAccounting accounting = indicatorAccountingDAO.get(IndicatorAccounting.class,
//										account.getIndicatorId());
//								String name = accounting.getName();
//								if (stringCellValue.indexOf(name) != -1) {
//									if ("320000000000000".equals(account.getAreaId())) {
//										cellJSValue.setCellValue(account.getEditValue());
//										cellJSIndicatorValue.setCellValue(nf.format(account.getEditValueGrowthRate()));
//									} else if ("320900000000000".equals(account.getAreaId())) {
//										cellYCValue.setCellValue(account.getEditValue());
//										cellYCIndicatorValue.setCellValue(nf.format(account.getEditValueGrowthRate()));
//									} else if ("320600000000000".equals(account.getAreaId())) {
//										cellNTValue.setCellValue(account.getEditValue());
//										cellNTIndicatorValue.setCellValue(nf.format(account.getEditValueGrowthRate()));
//									} else if ("320700000000000".equals(account.getAreaId())) {
//										cellLYGValue.setCellValue(account.getEditValue());
//										cellLYGIndicatorValue.setCellValue(nf.format(account.getEditValueGrowthRate()));
//									}
//								}
//							}
//						}
//						if (cellLYGValue.getCellType() == Cell.CELL_TYPE_NUMERIC
//								|| cellYCValue.getCellType() == Cell.CELL_TYPE_NUMERIC
//								|| cellNTValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
//							Double all = Double.valueOf("0");
//							if (cellLYGValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
//								all += Double.valueOf(cellLYGValue.getNumericCellValue());
//							}
//							if (cellYCValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
//								all += Double.valueOf(cellYCValue.getNumericCellValue());
//							}
//							if (cellNTValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
//								all += Double.valueOf(cellNTValue.getNumericCellValue());
//							}
//							cellSumValue.setCellValue(all);
//						}
//					}
//				}
			}
			wb.write(out);
		} catch (IOException e) {
			throw Lang.wrapThrow(e, "Excel文件错误");
		}
	}

	@Override
	public List<Map> getIndicatorProcess(String accountingResultId) {
		// TODO Auto-generated method stub
		return calcuIndicatorAccoutService.getIndicatorProcess(accountingResultId);
	}
}
